package com.specificgroup.jira.plugin.acpro.util;

import com.atlassian.jira.issue.issuetype.IssueType;
import com.atlassian.jira.issue.priority.Priority;
import com.atlassian.jira.issue.resolution.Resolution;
import com.atlassian.jira.issue.status.Status;
import com.atlassian.jira.project.version.Version;
import com.opensymphony.user.User;
import com.specificgroup.jira.plugin.acpro.exception.AcproXmlException;
import com.specificgroup.jira.plugin.acpro.mapping.*;
import com.specificgroup.jira.plugin.acpro.model.acpro.AcproItem;
import com.specificgroup.jira.plugin.acpro.model.acpro.AcproItemInfo;
import com.specificgroup.jira.plugin.acpro.xml.response.generated.AttachmentType;
import com.specificgroup.jira.plugin.acpro.xml.response.generated.CategoryType;
import com.specificgroup.jira.plugin.acpro.xml.response.generated.ItemType;
import com.specificgroup.jira.plugin.acpro.xml.response.generated.StatusType;
import com.specificgroup.jira.plugin.acpro.xml.response.generated.UrgencyType;
import com.specificgroup.jira.plugin.acpro.xml.response.generated.UserType;
import org.netbeans.lib.cvsclient.commandLine.command.status;

import java.util.List;

/**
 *
 * @author Ivan Holub
 */
public class ObjectsBuilder {

    private static final String FULL_NAME_SEPARATOR = " ";

    public static JiraUser buildJiraUser(User user) {
        JiraUser retValue = new JiraUser();
        retValue.setUserName(user.getName());
        retValue.setFullName(user.getFullName());
        retValue.setEmail(user.getEmail());
        return retValue;
    }

    public static JiraIssueType buildJiraIssueType(IssueType issueType) {
        JiraIssueType retValue = new JiraIssueType();
        retValue.setId(issueType.getId());
        retValue.setName(issueType.getName());
        return retValue;
    }

    public static JiraPriority buildJiraPriority(Priority priority) {
        JiraPriority retValue = new JiraPriority();
        retValue.setId(priority.getId());
        retValue.setName(priority.getName());
        return retValue;
    }

    public static JiraStatus buildJiraStatus(Status status) {
        JiraStatus retValue = new JiraStatus();
        retValue.setId(status.getId());
        retValue.setName(status.getName());
        return retValue;
    }

    public static JiraResolution buildJiraResolution(Resolution resolution) {
        JiraResolution retValue = new JiraResolution();
        retValue.setId(resolution.getId());
        retValue.setName(resolution.getName());
        return retValue;
    }

    public static AcproItem buildAcproItem(ItemType item) {
        AcproItem retValue = new AcproItem();
        retValue.setItemKey(item.getItemKey());
        retValue.setItemTitle(item.getItemTitle());
        return retValue;
    }

    public static AcproUser buildAcproUser(UserType user) {
        AcproUser retValue = new AcproUser();
        retValue.setUserID(user.getUserID());
        retValue.setUserKey(user.getUserKey());
        retValue.setEMail(user.getEMail());
        retValue.setFullName(user.getLastName() + FULL_NAME_SEPARATOR + user.getFirstName());
        return retValue;
    }

    public static AcproCategory buildAcproCategory(CategoryType category) {
        AcproCategory retValue = new AcproCategory();
        retValue.setKey(category.getKey());
        retValue.setName(category.getName());
        return retValue;
    }

    public static AcproUrgency buildAcproUrgency(UrgencyType urgency) {
        AcproUrgency retValue = new AcproUrgency();
        retValue.setKey(urgency.getKey());
        retValue.setName(urgency.getName());
        return retValue;
    }

    public static AcproStatus buildAcproStatus(StatusType status) {
        AcproStatus retValue = new AcproStatus();
        retValue.setKey(status.getKey());
        retValue.setName(status.getName());
        return retValue;
    }

    public static AcproItemInfo buildAcproItemInfo(ItemType item) throws AcproXmlException {
        AcproItemInfo retValue = new AcproItemInfo();
        retValue.setKey(item.getItemKey());
        retValue.setTitle(item.getItemTitle());
        ItemType.Attachments attachmentsType = item.getAttachments();
        if (attachmentsType != null) {
            List<AttachmentType> acproAttachments = attachmentsType.getAttachment();
            if (acproAttachments.size() > 0) {
                long size = 0;
                int linksNumber = 0;
                for (AttachmentType acproAttachment: acproAttachments) {
                    String sizeBytesStr = acproAttachment.getSizeBytes();
                    Long sizeBytes = AcproUtil.parseAcproSizeBytes(sizeBytesStr);
                    if (sizeBytes > 0) {
                        size += sizeBytes;
                    } else {
                        //sizeBytes="0" are Acpro links                        
                        linksNumber++;
                    }
                }
                if (size > 0) {
                    retValue.setAttachmentsSize(size);                    
                }
                if (linksNumber > 0) {
                    retValue.setLinksNumber(linksNumber);
                }
            }
        }
        
        return retValue;
    }

    public static JiraProjectVersion buildJiraProjectVersion(Version version) {
        JiraProjectVersion retValue = new JiraProjectVersion();
        retValue.setId(version.getId());
        retValue.setName(version.getName());
        return retValue;
    }

}
